package com.orangelife.mobile.parkingcard.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.parkingcard.adapter.ConditionListViewAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 商家搜索条件弹出框
 *
 * @author songhuangzhong
 */
public class ParkingTitlePopupWindow extends PopupWindow {

    private Context context;
    private List<String> data;

    private ListView listView;
    private TextView tvTitle;
    public OnPopupWindowItemClickListener listener;
    private ConditionListViewAdapter conditionListViewAdapter;

    /** 构造方法 */
    public ParkingTitlePopupWindow(Context context) {
        super(context);
        this.context = context;
        initData();
        initObject();
    }

    /**
     * 当点击item时设置标题的值
     *
     * @param tvTitle
     */
    public void setText(TextView tvTitle) {
        this.tvTitle = tvTitle;
    }

    /**
     * 设置数据
     */
    private void initData() {
        data = new ArrayList<String>();
        getShopInforCondition();
    }

    /**
     * 商家搜索条件
     */
    private void getShopInforCondition() {
        String s[] = context.getResources().getStringArray(
                R.array.parking_condition);
        for (int i = 0; i < s.length; i++) {
            data.add(s[i]);
        }
    }

    /**
     * 初始化popupwindow
     */
    private void initObject() {
        View contentView = LayoutInflater.from(context).inflate(
                R.layout.popwin_parking_condition, null);
        setContentView(contentView);
        setWidth(dip2px(14 * 4 + 20*2));
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        /* 设置popupwindow的基本属性 */
        listView = (ListView) contentView.findViewById(R.id.lvCondition);
        setAdapter();
        setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(R.color.white)));
        setFocusable(true);

        /* 设置捕获popupwindow外的点击焦点 */
        setOutsideTouchable(true);
        setTouchInterceptor(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    dismiss();
                    return true;
                }
                return false;
            }
        });
        // setListener();
    }

    private int dip2px(float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 载入列表item
     */
    private void setAdapter() {
        conditionListViewAdapter = new ConditionListViewAdapter(context, data);
        listView.setAdapter(conditionListViewAdapter);
    }

    /**
     * 设置popupwindow的适配器
     */
    public void setOnPopupWindowClickListener(
            OnPopupWindowItemClickListener listener) {
        this.listener = listener;
        conditionListViewAdapter.setListener(listener);
    }

    /**
     * 为列表的item添加监听
     */
    public interface OnPopupWindowItemClickListener {
        void onPopupWindowItemClick(int clickPositon, String strItemData);
    }

}